<template>
  <div class="user-center-container">
    <div class="user-center-content">
      <!-- 侧边栏 -->
      <div class="user-center-sidebar">
        <div class="user-avatar-container">
          <UserAvatar
            :user="userStore.userInfo"
            :size="80"
            show-status
          />
          <h3 class="user-name">{{ userStore.nickname }}</h3>
          <p class="user-email">{{ userStore.userInfo?.email }}</p>
        </div>
        
        <div class="sidebar-menu">
          <div 
            class="sidebar-item" 
            :class="{ active: activeTab === 'profile' }"
            @click="handleTabChange('profile')"
          >
            <n-icon :size="18">
              <PersonOutline />
            </n-icon>
            <span>个人资料</span>
          </div>
          <div 
            class="sidebar-item" 
            :class="{ active: activeTab === 'favorites' }"
            @click="handleTabChange('favorites')"
          >
            <n-icon :size="18">
              <HeartOutline />
            </n-icon>
            <span>我的收藏</span>
          </div>

          <div 
            class="sidebar-item" 
            :class="{ active: activeTab === 'settings' }"
            @click="handleTabChange('settings')"
          >
            <n-icon :size="18">
              <SettingsOutline />
            </n-icon>
            <span>账户设置</span>
          </div>
        </div>
      </div>
      
      <!-- 主内容区域 -->
      <div class="user-center-panel">
        <!-- 个人资料 -->
        <div v-show="activeTab === 'profile'" class="user-center-section">
          <div class="section-header">
            <n-icon :size="20" color="var(--n-color-primary)">
              <PersonOutline />
            </n-icon>
            <h2 class="section-title">个人资料</h2>
          </div>
          <ProfilePage />
        </div>
        
        <!-- 我的收藏 -->
        <div v-show="activeTab === 'favorites'" class="user-center-section">
          <div class="section-header">
            <n-icon :size="20" color="var(--n-color-primary)">
              <HeartOutline />
            </n-icon>
            <h2 class="section-title">我的收藏</h2>
          </div>
          <FavoritesList />
        </div>

        
        <!-- 账户设置 -->
        <div v-show="activeTab === 'settings'" class="user-center-section">
          <div class="section-header">
            <n-icon :size="20" color="var(--n-color-primary)">
              <SettingsOutline />
            </n-icon>
            <h2 class="section-title">账户设置</h2>
          </div>
          <AccountSettings />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import UserAvatar from '@/components/user/UserAvatar.vue'
import ProfilePage from './ProfilePage.vue'
import FavoritesList from './FavoritesList.vue'
import AccountSettings from './AccountSettings.vue'
import {
  PersonOutline,
  HeartOutline,
  SettingsOutline
} from '@vicons/ionicons5'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 当前激活的标签页
const activeTab = ref('profile')

// 处理标签页切换
const handleTabChange = (tab: string) => {
  activeTab.value = tab
  // 更新URL但不刷新页面
  router.replace({ 
    path: '/user/center', 
    query: { tab } 
  })
}

// 组件挂载时
onMounted(() => {
  // 从路由参数获取默认标签页
  const tab = route.query.tab as string
  if (tab && ['profile', 'favorites', 'settings'].includes(tab)) {
    activeTab.value = tab
  }
})
</script>

<style scoped>
.user-center-container {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
  min-height: calc(100vh - 120px);
}

.user-center-content {
  display: flex;
  gap: 24px;
  background: var(--n-card-color);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  min-height: 600px;
}

.user-center-sidebar {
  width: 260px;
  background: var(--n-color-primary-light-9);
  border-right: 1px solid var(--n-border-color);
  display: flex;
  flex-direction: column;
}

.user-avatar-container {
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  border-bottom: 1px solid var(--n-border-color);
  background: var(--n-card-color);
}

.user-name {
  margin: 12px 0 4px;
  font-size: 18px;
  font-weight: 600;
  color: var(--n-text-color-1);
}

.user-email {
  margin: 0;
  font-size: 13px;
  color: var(--n-text-color-3);
}

.sidebar-menu {
  flex: 1;
  padding: 8px 0;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 24px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: var(--n-text-color-2);
  position: relative;
}

.sidebar-item:hover {
  background: var(--n-color-primary-light-8);
  color: var(--n-color-primary);
}

.sidebar-item.active {
  color: var(--n-color-primary);
  background: var(--n-color-primary-light-8);
  font-weight: 500;
}

.sidebar-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--n-color-primary);
}

.user-center-panel {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.section-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: var(--n-text-color-1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .user-center-content {
    flex-direction: column;
  }
  
  .user-center-sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--n-border-color);
  }
  
  .sidebar-menu {
    display: flex;
    overflow-x: auto;
    padding: 0;
  }
  
  .sidebar-item {
    flex-shrink: 0;
    padding: 12px 16px;
    white-space: nowrap;
  }
  
  .sidebar-item.active::before {
    width: 100%;
    height: 3px;
    top: auto;
    bottom: 0;
    left: 0;
  }
  
  .user-center-panel {
    padding: 16px;
  }
}
</style>
